//
//  MJModifyNicknameView.m
//  MJ
//
//  Created by qiaoBu on 2017/12/8.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJModifyNicknameView.h"

@interface MJModifyNicknameView ()<UITextFieldDelegate>

@end
@implementation MJModifyNicknameView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = RGBColor(242,242,242);
        [self commonInit];
    }
    return self;
}
- (void)commonInit{
    
    UIView *backView = [[UIView alloc] init];
    [self addSubview:backView];
    backView.backgroundColor = [UIColor whiteColor];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(55);
    }];
    
    UIView *topLine = [[UIView alloc] init];
    [self addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    topLine.backgroundColor = RGBColor(229,229,229);
    
    UIView *bottomLine = [[UIView alloc] init];
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.mas_equalTo(backView);
        make.height.mas_equalTo(0.5);
    }];
    bottomLine.backgroundColor = RGBColor(229,229,229);
    
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backView addSubview:clearBtn];
    [clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(backView.mas_right).offset(-20);
        make.centerY.mas_equalTo(backView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [clearBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    [clearBtn addTarget:self action:@selector(clearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _nameField = [[UITextField alloc] init];
    [backView addSubview:_nameField];
    [_nameField mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(backView.mas_left).offset(20);
        make.right.mas_equalTo(clearBtn.mas_left).offset(-20);
        make.centerY.mas_equalTo(backView.mas_centerY);
        make.height.mas_equalTo(backView.mas_height);
    }];
    _nameField.delegate = self;
    _nameField.textAlignment = NSTextAlignmentLeft;
    _nameField.font = [UIFont systemFontOfSize:16];
    _nameField.textColor = RGBColor(140,140,140);
    [_nameField isFirstResponder];

    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:MJ_Name];
    if (name.length ==0) {
      
         _nameField.text = @"神秘人";
        
    }else{
        
         _nameField.text = name;
    }
    
    _modifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_modifyBtn];
    [_modifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.height.mas_equalTo(85/2.0);
        make.top.mas_equalTo(backView.mas_bottom).offset(20);
    }];
    [_modifyBtn addTarget:self action:@selector(modifyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_modifyBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [_modifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [_modifyBtn setBackgroundImage:[UIImage imageNamed:@"huidi"] forState:UIControlStateNormal];
//    [_modifyBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateSelected];
    _modifyBtn.clipsToBounds = YES;
    _modifyBtn.layer.cornerRadius = 85/4.0;
    _modifyBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    _modifyBtn.backgroundColor = RGBColor(177, 177, 177);
    _modifyBtn.enabled = NO;
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    _modifyBtn.backgroundColor = RGBColor(255,130,128);
//    [_modifyBtn setBackgroundImage:[UIImage imageNamed:@"huidi"] forState:UIControlStateNormal];
    _modifyBtn.enabled = YES;

}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
//    if (textField.text)
//    {
//         _modifyBtn.backgroundColor = RGBColor(255,130,128);
//    }
//    else
//    {
//         _modifyBtn.backgroundColor = RGBColor(191,191,191);
//    }
}
- (void)clearBtnClick:(UIButton *)button{
    
    _nameField.text = nil;

}
- (void)modifyBtnClick:(UIButton *)button{
    
    if (self.modifyBlcok)
    {
        self.modifyBlcok(_nameField.text);
    }
}


@end
